package com.example.feign;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import feign.Feign;
import feign.Target;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.okhttp.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态url
 * http://cloud.spring.io/spring-cloud-static/Camden.SR6/#_creating_feign_clients_manually
 * https://stackoverflow.com/questions/43733569/how-can-i-change-the-feign-url-during-the-runtime
 */
@RestController
@RequestMapping("httpDyna")
public class HttpDynaController {


    @Autowired
    private HttpDynaService httpDynaService;



    @RequestMapping("/getGoodsList/{sysId}/{parentId}")
    public String getGoodsList(@PathVariable("sysId") String sysId,@PathVariable("parentId") String parentId) throws URISyntaxException {
        Map<String,Object> map=new HashMap<>();
        map.put("sysId",sysId);
        map.put("parentId",parentId);
        StringBuffer url=new StringBuffer("http://hqb.t.nxin.com/openservice/getGoodsList.shtml");
//        url.append("?");
//        for(String key:map.keySet()){
//            url.append(key+"="+map.get(key));
//            url.append("&");
//        }
//        String reqUrl=url.toString().substring(0,url.length()-1);
////        return httpDynaConsumeService.getGoodsList(new URI("http://hqb.t.nxin.com"),map);
        return httpDynaService.headRequest(new URI(url.toString()),map);
//        return httpDynaService.getGoodsList(new URI(url.toString()),map,map);
    }

    @RequestMapping("/getToken")
    public String getToken() throws URISyntaxException {
        Map<String,Object> map=new HashMap<>();
        map.put("grant_type","client_credentials");
        map.put("scope","read");
        map.put("client_id","NX-I-005");
        map.put("client_secret","03390f7cc57d6eec10f2d78bc4f46099");
        StringBuffer url=new StringBuffer("http://yqt.i.t.nxin.com/oauth/token");
        url.append("?");
        for(String key:map.keySet()){
            url.append(key+"="+map.get(key));
            url.append("&");
        }
        String reqUrl=url.toString().substring(0,url.length()-1);
//        return httpDynaConsumeService.getGoodsList(new URI(reqUrl));
        return httpDynaService.postGoodsList(new URI(reqUrl));
    }

    @RequestMapping(value = "/qryBalByDate",method = RequestMethod.POST)
    public String qryBalByDate() throws URISyntaxException {
        Map<String,Object> map=new HashMap<>();
        map.put("access_token","716b6423-d89b-4154-82d1-c5138d72c189");
        StringBuffer url=new StringBuffer("http://localhost:8080/api/read/qryBalByDate");
//        url.append("?");
//        for(String key:map.keySet()){
//            url.append(key+"="+map.get(key));
//            url.append("&");
//        }
//        String reqUrl=url.toString().substring(0,url.length()-1);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("acntNo","600033045");
        jsonObject.put("bankType","CMBC");
        jsonObject.put("clientId","2200003220");
        jsonObject.put("qryDate","2018-08-08");
        JSONArray jsonArray=new JSONArray();
        jsonArray.add(jsonObject);
        JSONObject obj=new JSONObject();
        obj.put("list",jsonArray);
        Map<String,Object> headmap=new HashMap<>();
        headmap.put("grant_type","client_credentials");
        headmap.put("scope","read");
//        return httpDynaConsumeService.qryBalByDate(new URI(reqUrl),obj);
//        return httpDynaService.qryBalByDate(new URI(reqUrl),obj);
        return httpDynaService.qryBalByDate(new URI(url.toString()),map,obj,headmap);
    }
}
